#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/livepatch.h>

#include <linux/seq_file.h>


static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
{
	seq_printf(m, "%s\n", "hello!!!");


	return 0;
}

static struct klp_func funcs[] = {
	{
		.old_name = "cmdline_proc_show",
		.new_func = livepatch_cmdline_proc_show,
	}, { }
};


static struct klp_object objs[] = {
	{
		.funcs = funcs,
	}, { }
};

static struct klp_patch klpatch = {
	.mod = THIS_MODULE,
	.objs = objs,
};

static int livepatch_init(void)
{
	int ret;

	ret = klp_register_patch(&klpatch);
	if (ret)
		return ret;

	ret = klp_enable_patch(&klpatch);
	if (ret) {
		WARN_ON(klp_unregister_patch(&klpatch));
		return ret;
	}

	return 0;
}


/* note:
 * you need 'echo 0 > /sys/kernel/livepatch/livepatch_demo/enabled' before rmmod this module.
 */
static void livepatch_exit(void)
{
	int ret;

	pr_info("%s---%d\n", __func__, __LINE__);
	ret = klp_disable_patch(&klpatch);
	if (ret) {
		pr_info("klp_disable_patch failed\n");
	}

	WARN_ON(klp_unregister_patch(&klpatch));
}

module_init(livepatch_init);
module_exit(livepatch_exit);

MODULE_LICENSE("GPL");

MODULE_INFO(livepatch, "Y");

